package com.fish.anne.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.fish.anne.service.SystemRegionManager;
import com.fish.anne.model.SystemRegion;
import com.fish.anne.webapp.action.BaseAction;

import java.util.List;

public class SystemRegionAction extends BaseAction implements Preparable {
	private SystemRegionManager systemRegionManager;
	private List<SystemRegion> systemRegions;
	private SystemRegion systemRegion;
	private Long regionId;

	public void setSystemRegionManager(SystemRegionManager systemRegionManager) {
		this.systemRegionManager = systemRegionManager;
	}

	public List<SystemRegion> getSystemRegions() {
		return systemRegions;
	}

	/**
	 * Grab the entity from the database before populating with request
	 * parameters
	 */
	public void prepare() {
		if (getRequest().getMethod().equalsIgnoreCase("post")) {
			// prevent failures on new
			String systemRegionId = getRequest().getParameter("systemRegion.regionId");
			if (systemRegionId != null && !systemRegionId.equals("")) {
				systemRegion = systemRegionManager.get(new Long(systemRegionId));
			}
		}
	}

	public String list() {
		systemRegions = systemRegionManager.getAll();
		return SUCCESS;
	}

	public void setRegionId(Long regionId) {
		this.regionId = regionId;
	}

	public SystemRegion getSystemRegion() {
		return systemRegion;
	}

	public void setSystemRegion(SystemRegion systemRegion) {
		this.systemRegion = systemRegion;
	}

	public String delete() {
		systemRegionManager.remove(regionId);
		saveMessage(getText("systemRegion.deleted"));

		return SUCCESS;
	}

	public String edit() {
		if (regionId != null) {
			systemRegion = systemRegionManager.get(regionId);
		} else {
			systemRegion = new SystemRegion();
		}

		return SUCCESS;
	}

	public String save() throws Exception {

		boolean isNew = (systemRegion.getRegionId() == null);

		if (isNew) {
			systemRegion.setPackage_("");
			systemRegion.setParentId(0L);
			systemRegion.setRegionGrade(0L);
			systemRegion.setRegionPath("");

		}else{
			SystemRegion _systemRegion = this.systemRegionManager.get(systemRegion.getRegionId());
			_systemRegion.setLocalName(this.systemRegion.getLocalName());
			_systemRegion.setShowOrder(systemRegion.getShowOrder());
			systemRegion = _systemRegion;
		}

		systemRegionManager.save(systemRegion);

		 
		saveMessage("操作成功");

		return SUCCESS;

	}
}